﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using eCommerce.Core;
using eCommerce.Core.Domain.Products;
using eCommerce.Models.Product;
using eCommerce.Services.Messages;
using eCommerce.Services.Products;
using ProductInfoInsertRequest = eCommerce.Models.Product.ProductInfoInsertRequest;

namespace eCommerce.Controllers.api
{
    public class ProductVariantAttributeSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }

    public class ProductInfoSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }

   

    public class ProductVariantInfoSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }

    public class ProductVariantInfoSaveRequest
    {
        public int Id{ get; set; }
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Sku{ get; set; }


        public decimal Price { get; set; }
        public int DisplayOrder { get; set; }
        public int StockQuantity { get; set; }

        public int PictureId { get; set;}
        public bool Published { get; set; }
        
    }


    public class ProductApiController : ApiController
    {

        private IProductService _productService;


        private IWorkContext _workContext;
        private IMessageService _messageService;


        public ProductApiController(IProductService productService,  IWorkContext workContext, IMessageService messageService)
        {
            _productService = productService;
            _workContext = workContext;
            _messageService = messageService;
        }

        //[HttpPost]
        //public ProductDeleteResult DeleteProduct(ProductDeleteRequest request)
        //{
        //    var result = new ProductDeleteResult();
        //    try
        //    {
        //        _productService.DeleteProduct(request.ProductId);


        //        result.Status = ConstInfo.ResultSuccessStatus;
        //        result.Message = _messageService.GetResourceValue(Messages.ProductDeleteSuccess, 0);

        //        //_activityService.InsertActivity("InsertProductVariantAttributeValue", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
        //        return result;
        //    }
        //    catch (Exception e)
        //    {
        //        result.Status = ConstInfo.ResultFailedStatus;
        //        result.Message = e.Message;
        //        return result;
        //    }
        //}


        //[HttpPost]
        //public ProductInfoSaveResult UpdateProductInfo(ProductInfoUpdateRequest request)
        //{
        //    var result = new ProductInfoSaveResult();


        //    try
        //    {

        //        var updateProductInfoRequest = new ProductInfoUpdateRequest()
        //        {
        //            Id = request.Id,
        //            ProductName = request.ProductName,
        //            FullDescription = request.FullDescription,
        //            IsPublished = request.IsPublished,
        //            ShortDescription = request.ShortDescription
        //        };
        //        var updateResult = _productService.UpdateProductInfo(updateProductInfoRequest);

        //        result.Status = ConstInfo.ResultSuccessStatus;
        //        result.Message = _messageService.GetResourceValue(Messages.ProductUpdateInfoSuccess, 0);
        //        result.Id = updateResult.Id;

        //        _activityService.InsertActivity("updateproductinfo", JsonHelper.ToJson(updateProductInfoRequest), _workContext.CurrentCustomer);
        //        //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));

        //        return result;
        //    }
        //    catch (Exception e)
        //    {
        //        result.Status = ConstInfo.ResultFailedStatus;
        //        result.Message = e.ToString();
        //        return result;
        //    }

        //}

        [HttpPost]
        public ItemSaveResult InsertItem(ItemCatalogsModel request)
        {
            var result = new ItemSaveResult();


            try
            {
                var itemGroups = new List<ItemGroup>();
                foreach (var itemCatalogGroup in request.ItemCatalogGroups)
                {
                    var items = itemCatalogGroup.ItemCatalogs.Select(itemCatalog => new Item()
                    {
                        ProductId = request.ProductId, 
                        ItemGroupCode = string.Format("{0}{1}",
                        request.ProductId,
                        itemCatalogGroup.ProductAttributeValueId),
                        CreatedOn = DateTime.Now, 
                        UpdatedOn = DateTime.Now, 
                        Deleted = false, 
                        ItemAttributeValueId = itemCatalog.ItemAttributeValueId,
                        Price = itemCatalog.Price, 
                        StockQuantity = itemCatalog.Stock, 
                        ProductAttributeValueId = itemCatalog.ProductAttributeValueId,
                       Published = true,
                    }).ToList();



                    var itemGroup = new ItemGroup()
                    {
                        ItemGroupNo = string.Format("{0}{1}",
                            request.ProductId, 
                            itemCatalogGroup.ProductAttributeValueId),
                        ProductAttributeValueId = itemCatalogGroup.ProductAttributeValueId,
                        IconPictureId = itemCatalogGroup.IconPictureId,
                        ItemGroupPictures = BuildItemGroupPictures(itemCatalogGroup.ProductPictures),
                        Items = items,
                        ProductId = request.ProductId, 
                    };
                    itemGroups.Add(itemGroup);
                   
                }



                _productService.InsertItems(request.ProductId, itemGroups);


                //if (request.ProductAttributes != null)
                //{
                //    foreach (var productAttributeValueModel in request.ProductAttributes)
                //    {
                //        _productService.CreateProductAttributeValue(itemId, productAttributeValueModel.Value, productAttributeValueModel.ProductAttributeKeyId);
                //    }
                //}




                result.Status = "0";
                //result.Id = insertResult.Id;
                //result.Message = _messageService.GetResourceValue(Messages.ProductCreateInfoSuccess, 0);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.ToString();
                return result;
            }

        }

        private IList<ItemGroupPicture> BuildItemGroupPictures(IList<ProductPicModel> productPictures)
        {
            if (productPictures != null)
            {
                return productPictures.Select(c => new ItemGroupPicture()
                {
                    PictureId = c.Id
                }).ToList();
            }
            return null;
        }

        [HttpPost]
        public ProductInfoSaveResult InsertProductInfo(ProductInfoInsertRequest request)
        {
            var result = new ProductInfoSaveResult();


            try
            {



                var productId = _productService.InsertProduct(request.ShopId,
                    request.ProductName,
                    request.ShortDescription,
                    request.FullDescription,
                    request.Price,
                    request.StockQuantity,
                    request.UseProductAttributeKey,
                    request.UseItemAttributeKey,
                    request.ProductAttributeKey.Id,
                    request.ItemAttributeKey.Id,
                    request.BrandId,
                    request.CategoryId,
                    request.PictureId,
                    request.ProductAttributeValues,
                    request.ItemAttributeValues);

               
               

                result.Id = productId;
                result.Status = "0";
                //result.Id = insertResult.Id;
                //result.Message = _messageService.GetResourceValue(Messages.ProductCreateInfoSuccess, 0);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.ToString();
                return result;
            }

        }
        
    }

    public class ItemInsertRequest
    {
        public int ProductId { get; set; }
        public decimal Price { get; set; }
        public int StockQuantity { get; set; }
        public int PictureId { get; set; }
        public ProductAttributeValueModel ProductAttribute { get; set; }
        public ItemAttributeValueModel ItemAttribute { get; set; }

        public bool UseProductAttribute { get; set; }

        public bool UseItemAttribute { get; set; }
     
    }

    public class ItemSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }

    public class ProductDeleteRequest
    {
        public int ProductId { get; set; }
    }

    public class ProductDeleteResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
}
